<template>
	<view class="container">
		<view class="drill-content">
			<view class="drill_wk-title u-flex ">
				<view class="title-line"></view>
				<view class="title-txt">选择钻机</view>
			</view>
			<view class="drill_wk-id-info container white-bg z-b-r u-margin-top-20 u-flex u-flex-wrap">
				<view class="drill-item u-margin-left-30 u-margin-bottom-20" v-for="(item,index) in drills" :key="item">
					<u-tag :text="item" :type="curDrillID=== item ? 'success' : 'info'" @click="selectDrill(item)"/>
				</view>
			</view>
		</view>
		<view class="wk-content u-margin-top-40">
			<view class="drill_wk-title u-flex ">
				<view class="title-line"></view>
				<view class="title-txt">选择员工</view>
			</view>
			<view class="drill_wk-id-info container white-bg z-b-r u-margin-top-20 u-flex u-flex-wrap">
				<view class="drill-item u-margin-left-30 u-margin-bottom-20" v-for="(item,index) in workerInfo" :key="item.wkID">
					<u-tag :text="item.wkID+' : '+item.name" :type="curWkID === item.wkID ? 'success' : 'info'" @click="selectWK(item)"/>
				</view>
			</view>
		</view>
		<!-- 消息提示 -->
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>
		<!-- 底部导航 -->
		<bottom-bar>
			<u-button :disabled="disabledBtn" type="primary" @click="confirmBindHandle">提交</u-button>
		</bottom-bar>
		<!-- 模态框 -->
		<u-modal v-model="showModal" show-cancel-button="true" @confirm="confirmUpdateDrill" content="该员工已经分配其他钻机,是否确认更改!"></u-modal>
	</view>
</template>

<script>
	import {mapState} from "vuex"
	export default {
		data() {
			return {
				drills:[],
				projectID:"",
				projectInfo:{},
				disabledBtn:false,
				curDrillID:"",
				curWkID:"",
				showModal:false,
				workerInfo:[]
			}
		},
		onLoad(params) {
			this.projectID = params.projectID;
			this.drills = params.drillID.split(",")
			this.getProjectInfo()
		},
		computed:{
			...mapState({
				token:"token"
			})
		},
		methods: {
			selectDrill(item){
				this.curDrillID = item;
			},
			selectWK(item){
				this.curWkID = item.wkID;
			},
			confirmBindHandle(){
				let {projectInfo,curDrillID,curWkID} = this;
				// 1、是否已经选择员工和钻机
				if(curDrillID === "" || curWkID === ""){
					this.$refs.uTips.show({
						title: '员工和钻机缺一不可！',
						type: 'warning'
					})
					return false;
				}
				
				// 2、判断当前钻机是否已经分配给此员工
				let curWKInfo = {}
				projectInfo.worker_ids.forEach((item,index)=>{
					if(item.wkID === curWkID){
						curWKInfo = item;
					}
				})
				if(curWKInfo.drillID === curDrillID){
					this.$refs.uTips.show({
						title: '该员工已经分配此钻机！',
						type: 'warning'
					})
					return false;
				}
				//3、判断是否已经分配钻机，但不是当前选择的钻机
				if(curWKInfo.drillID !== "" && curWKInfo.drillID !== curDrillID){
					this.showModal = true;
					return false;
				}
				
				//
				this.sendBindDrillRequset();
			},
			//更改当前已经分配的钻机
			confirmUpdateDrill(){
				this.sendBindDrillRequset();
			},
			async sendBindDrillRequset(){
				let {projectInfo,curDrillID,curWkID} = this;
				this.$loading("正在分配");
				this.disabledBtn = true;
				let res = await this.$network({
					api:"main",
					data:{
						action:"drillAdmin/drillBindWorker",
						data:{
							wkID:curWkID,
							drillID:curDrillID
						}
					}
				})
				
				uni.hideLoading();
				if(res.status === 0){
					this.curDrillID = "";
					this.curWkID = "";
					this.disabledBtn = false;
					this.$refs.uTips.show({
						title: '绑定成功',
						type: 'success'
					})
				}
			},
			async getProjectInfo(){
				let {projectID} = this;
				this.$loading("正在加载")
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/getProjectDetail",
						data:{
							id:projectID
						}
					}
				})
				if(res.status === 0){
					this.projectInfo = res.data[0];
				}
				
				let getWKinfoTasks = [];
				this.projectInfo.worker_ids.forEach((item,index)=>{
					getWKinfoTasks.push(this.$network({
						api:"main",
						data:{
							action:"user/getUserInfoByWkID",
							data:{
								wkID:item.wkID
							}
						}
					}))
				})

				let wkInfoRes = await Promise.all(getWKinfoTasks);
				uni.hideLoading();
				wkInfoRes.forEach(item=>{
					if(item.status === 0){
						this.workerInfo.push(item.data[0])
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.title-line{
		width: 6rpx;
		height: 30rpx;
		background-color: #333333;
		border-radius: 10rpx;
		margin-right: 15rpx;
		margin-left: 20rpx;
	}
	.drill_wk-title{
		line-height: 40rpx;
		font-weight: bold;
		font-size: 32rpx;
		color: #333333;
	}
</style>
